﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using WinHBM.Utils;

/**
 * http://blog.chinaunix.net/uid-20551990-id-5680682.html
 *https://www.cnblogs.com/ChYQ/p/6825908.html
 */

namespace WinHBM.Utils
{
    class UtilSoftKeyBoard
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(UtilSoftKeyBoard));
        // 申明要使用的dll和api
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
        public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);


        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        static System.Diagnostics.Process softKey = null;

        public static void openSoftKey()
        {
           
            try
            {

                string platform = "win7";
                int osVersion = UtilOS.VersionCode();
                if(osVersion >= 6 && osVersion < 10)
                {
                    platform = "win7";
                }else if(osVersion >= 10)
                {
                    platform = "win10";
                }

                string oskPath = System.Environment.CurrentDirectory + "\\osk-" + platform + ".exe";

                if (!System.IO.File.Exists(oskPath))
                {
                    MessageBox.Show("软件盘可执行文件不存在！");
                    return;
                }
                Process process = findProcess();
                if (process != null)
                {
                    Logger.Debug("键盘已启动");
                    return;
                }

                if (softKey == null || softKey.HasExited)
                {



                    Process p = new Process();

                    try
                    {
                        p.StartInfo.FileName = oskPath;
                        //p.StartInfo.RedirectStandardInput = true;
                        //p.StartInfo.RedirectStandardOutput = true;
                        //p.StartInfo.RedirectStandardError = true;
                        //p.StartInfo.UseShellExecute = false;
                        p.StartInfo.Verb = "RunAs";
                        //p.StartInfo.UseShellExecute = false;
                        p.Start();

                        // 获取屏幕尺寸
                        int iActulaWidth = Screen.PrimaryScreen.Bounds.Width;
                        int iActulaHeight = Screen.PrimaryScreen.Bounds.Height;


                        // 设置软键盘的显示位置，底部居中
                        int posX = (iActulaWidth - 1000) / 2;

                        int posY = (iActulaHeight - 400);


                        IntPtr intptr = IntPtr.Zero;
                        //设定键盘显示位置
                        MoveWindow(intptr, posX, posY, 1000, 300, true);


                        //设置软键盘到前端显示
                        //  SetForegroundWindow(intptr);
                    }
                    catch (Exception e)
                    {
                        Logger.Debug(e.Message.ToString());
                    }



                    /*while (IntPtr.Zero == intptr)
                   {
                       System.Threading.Thread.Sleep(100);
                    intptr = FindWindow(null, "屏幕键盘");
                    }*/
                }

            }
            catch (System.Exception ex)
            {
                Logger.Error(ex);
            }

        }

        private static Process findProcess()
        {
            System.Diagnostics.Process[] myPs = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process p in myPs)
            {
                if (p.ProcessName == "osk" && !p.HasExited)
                {
                    return p;
                }
            }
            return null;
        }

        public static void closeSoftKey()
        {
            try {
                Process process = findProcess();
                if(process != null)
                {
                    process.Kill();
                    process.Close();
                }
            }
            catch (System.Exception ex)
            {
                Logger.Error(ex);
            }
           
        }

    }
}
